package com.app.util.type;

public class TypeUtil {

	public static boolean isPrimitive(Object obj) {
		if (obj instanceof Boolean//
				|| obj instanceof Byte//
				|| obj instanceof Character//
				|| obj instanceof Short//
				|| obj instanceof Integer//
				|| obj instanceof Long//
				|| obj instanceof Float//
				|| obj instanceof Double//
		) {
			return true;
		}
		return obj.getClass().isPrimitive();
	}
 
	public static Object toTypeObject(Object o, String type) {
		if (null == o)
			return o;
		if (Boolean.class.getSimpleName().equalsIgnoreCase(type)) {
			return Boolean.parseBoolean(o.toString());
		}
		if ("char".equalsIgnoreCase(type) || Character.class.getSimpleName().equalsIgnoreCase(type)) {
			if (o.toString().length() == 0) {
				return new Character('\0');
			}
			return new Character(o.toString().charAt(0));
		}
		if (Byte.class.getSimpleName().equalsIgnoreCase(type)) {
			return Byte.parseByte(o.toString());
		}
		if ("int".equalsIgnoreCase(type) || Integer.class.getSimpleName().equalsIgnoreCase(type)) {
			return Integer.parseInt(o.toString());
		}
		if (Long.class.getSimpleName().equalsIgnoreCase(type)) {
			return Long.parseLong(o.toString());
		}
		if (Float.class.getSimpleName().equalsIgnoreCase(type)) {
			return Float.parseFloat(o.toString());
		}
		if (Double.class.getSimpleName().equalsIgnoreCase(type)) {
			return Double.parseDouble(o.toString());
		}
		if (String.class.getSimpleName().equalsIgnoreCase(type)) {
			return o.toString();
		}
		return o;
	}

}
